--
-- Author: yawei.zhu
-- Date: 2018-10-09 20:57:50
--
-- BossTeamRankRender
--

--local BossTeamRankMoreRender = import(".BossTeamRankMoreRender")
local BossTeamRankLegionRender = import(".BossTeamRankLegionRender")

local ClassRef = app.mvc.createRenderClass(...)

--data =
function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.BossTeamRankRender))
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end
function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

--
function ClassRef:init(data)
    self.super.init(self, data)

    self._panelLegionClass = self._ui["Panel/Panel_legion_class"] --职业图标
    self._panelLegionClassPosX = self._panelLegionClass:getPositionX()
    self._panelLegionClassWidth = self._panelLegionClass:getContentSize().width
    for i = 1, 5 do
        self["class"..i] = self._ui["Panel/class_"..i]
    end
    for i = 1, 3 do
        self["rank"..i] = self._ui["Panel_"..i]
    end
    self._ui.num:setVisible(true)
    self.myBG = self._ui["Panel/Panel_bg/bg_my"]
    self.button = self._ui.Button_1
    self.btnName = self.button:getChildByName("name")
    
    self.button:addClickEventListener(function()    
        if self.button:isBright() then 
            executeFunc(self._onClick, "onShowExtend", self)
        else            
            executeFunc(self._onClick, "onHideExtend", self)
        end
    end)

    self:setContentSize(self._ui.Panel:getContentSize())
    self:refresh()
end


function ClassRef:refresh()
    if not self._data then return end
    self:clearItem()

    local rank = self._data[1]
    local team = self._data[2]  
    local teamInfo = team.teamInfo
    local isMy = false
    local leaderName = ""
    local memberNum = #teamInfo.teamMembers --队伍总人数

    -- dump(teamInfo.teamMembers)

    for i, member in ipairs(teamInfo.teamMembers) do
        if member.isLeader then
            leaderName = member.userBaseInfo.name
        end
        if member.userBaseInfo.uid == UD:getUid() then
            -- print("1111111")
            isMy = true
        end

        local classIconPath = GD:getClassIcon( member.userBaseInfo.classID, 2 )
        if memberNum <= 5 then
            self["class"..i]:setVisible(true)
            self["class"..i]:loadTexture(classIconPath)
        else
            self._panelLegionClass:getChildByName("class_"..i):setVisible(true)
            self._panelLegionClass:getChildByName("class_"..i):loadTexture(classIconPath)
            --设置职业图标位置（超过5人的时候才设置）
            if memberNum > 5 then
                self._panelLegionClass:getChildByName("class_"..i):setPositionX((i-1)*(self._panelLegionClassWidth/memberNum))
            end
        end
        self._panelLegionClass:setVisible(true)
    end

    self._ui.name:setString(string.format(L("bossTeamRank_team"), leaderName))

    if rank <= 3 then
        self["rank"..rank]:setVisible(true)
    else
        self._ui.num:setString(rank)
    end

    self._ui.combat:setString(team.teamCombat)

    local timeData = os.date("*t", team.battleTime)
    local szTime = string.format("%d:%02d:%02d", timeData.hour, timeData.min, timeData.sec)
    self._ui.time:setString(szTime)

    -- 如果是我的队伍
    if false then
        self.myBG:setVisible(true)
        local data = {}
        data.rank = rank
        data.time = szTime 
        executeFunc(self._onClick, "onShowMySelf", data)
    end
end

function ClassRef:hideDetail()
    self.button:setBright(true)
    self.btnName:setString(L("bossTeamRank_expand"))

    if self._detailNode then
        self._detailNode:setVisible(false)
    end
    self._ui.Panel:setPositionY(0)
    self:setContentSize(self._ui.Panel:getContentSize())
end

function ClassRef:showDetail()
    self.button:setBright(false)
    self.btnName:setString(L("bossTeamRank_putaway"))

    if not self._detailNode then
        --self._detailNode = BossTeamRankMoreRender.create()
        self._detailNode = BossTeamRankLegionRender.create()
        self._detailNode:setData(self._data[2].teamInfo.teamMembers)
        self._detailNode:setPosition(cc.p(0, 0))
        self:addChild(self._detailNode, -1)
    end
    self._detailNode:setVisible(true)

    local size = self._ui.Panel:getContentSize()
    local nodeSize = self._detailNode:getContentSize()
    self._ui.Panel:setPositionY(nodeSize.height)
    self:setContentSize(cc.size(size.width, size.height + nodeSize.height))
end

function ClassRef:clearItem()
    self.myBG:setVisible(false)
    self._ui.num:setString("")
    for i = 1, 5 do
        self["class"..i]:setVisible(false)
    end
    for i = 1, 3 do
        self["rank"..i]:setVisible(false)
    end
    for i = 1, 10 do
        self._panelLegionClass:getChildByName("class_"..i):setVisible(false)
    end
end

function ClassRef:onBtnClick(cb)
    self._onClick = cb
end

return ClassRef
